﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Library;

namespace Sport_Shop
{
    public partial class KQTim : System.Web.UI.Page
    {
        
        static PagedDataSource p = new PagedDataSource();
        public static int intSTT;
        public static int trang_thu = 0;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load_KQTim();
                
            }
        }

        private void load_KQTim()
        {
            try
            {
                if (Request.QueryString["TenSP"] != null)
                {
                    DataAccess da = new DataAccess();
                    string ten = Request.QueryString["TenSP"].ToString();
                    string sql = "SELECT * FROM SanPham WHERE TenSP LIKE '%" + ten + "%'";
                    p.DataSource = da.GetDataList(sql).Tables[0].DefaultView;
                    p.PageSize = 4;
                    p.CurrentPageIndex = trang_thu;
                    p.AllowPaging = true;

                    btDau.Enabled = true; btTruoc.Enabled = true; btSau.Enabled = true; btCuoi.Enabled = true;

                    if (p.IsFirstPage == true)
                    {
                        btDau.Enabled = false;
                        btTruoc.Enabled = false;
                        btSau.Enabled = true;
                        btCuoi.Enabled = true;
                    }
                    if (p.IsLastPage == true)
                    {
                        btDau.Enabled = true;
                        btTruoc.Enabled = true;
                        btSau.Enabled = false;
                        btCuoi.Enabled = false;
                    }
                    tbTrang.Text = (trang_thu + 1) + "/" + p.PageCount;
                    dlKQTim.DataSource = p;
                    dlKQTim.DataBind();
                }//Tìm theo TenSP

                if (Request.QueryString["GiaTu"] != null && Request.QueryString["GiaDen"] != null)
                {
                    DataAccess da = new DataAccess();
                    string giatu = Request.QueryString["GiaTu"].ToString();
                    string giaden = Request.QueryString["GiaDen"].ToString();
                    string sql = "SELECT * FROM SanPham WHERE DonGia BETWEEN " + giatu + " AND " + giaden;
                    p.DataSource = da.GetDataList(sql).Tables[0].DefaultView;
                    p.PageSize = 4;
                    p.CurrentPageIndex = trang_thu;
                    p.AllowPaging = true;

                    btDau.Enabled = true; btTruoc.Enabled = true; btSau.Enabled = true; btCuoi.Enabled = true;

                    if (p.IsFirstPage == true)
                    {
                        btDau.Enabled = false;
                        btTruoc.Enabled = false;
                        btSau.Enabled = true;
                        btCuoi.Enabled = true;
                    }
                    if (p.IsLastPage == true)
                    {
                        btDau.Enabled = true;
                        btTruoc.Enabled = true;
                        btSau.Enabled = false;
                        btCuoi.Enabled = false;
                    }
                    tbTrang.Text = (trang_thu + 1) + "/" + p.PageCount;
                    dlKQTim.DataSource = p;
                    dlKQTim.DataBind();
                }//Tim theo Gia


                if (Request.QueryString["TenSP"] != null && Request.QueryString["GiaTu"] != null && Request.QueryString["GiaDen"] != null)
                {
                    DataAccess da = new DataAccess();
                    string ten = Request.QueryString["TenSP"].ToString();
                    string giatu = Request.QueryString["GiaTu"].ToString();
                    string giaden = Request.QueryString["GiaDen"].ToString();
                    string sql = "SELECT * FROM SanPham WHERE TenSP LIKE '%" + ten + "%' AND DonGia >= " + giatu + " AND DonGia <= " + giaden;
                    p.DataSource = da.GetDataList(sql).Tables[0].DefaultView;
                    p.PageSize = 4;
                    p.CurrentPageIndex = trang_thu;
                    p.AllowPaging = true;

                    btDau.Enabled = true; btTruoc.Enabled = true; btSau.Enabled = true; btCuoi.Enabled = true;

                    if (p.IsFirstPage == true)
                    {
                        btDau.Enabled = false;
                        btTruoc.Enabled = false;
                        btSau.Enabled = true;
                        btCuoi.Enabled = true;
                    }
                    if (p.IsLastPage == true)
                    {
                        btDau.Enabled = true;
                        btTruoc.Enabled = true;
                        btSau.Enabled = false;
                        btCuoi.Enabled = false;
                    }
                    tbTrang.Text = (trang_thu + 1) + "/" + p.PageCount;
                    dlKQTim.DataSource = p;
                    dlKQTim.DataBind();
                }//Tim theo Gia & Ten

                //else
                //{
                //    DataAccess da = new DataAccess();
                //    string tenloai = Request.QueryString["TenLoai"].ToString();
                //    string sql = "SELECT * FROM SanPham sp, LoaiSanPham lsp WHERE sp.MaLoai = lsp.MaLoai AND TenLoai = '" + tenloai + "'";
                //    dlKQTim.DataSource = da.GetDataList(sql).Tables[0];
                //    dlKQTim.DataBind();
                //}



            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.ToString());
            }


                


                //if (Request.QueryString["LoaiSP"] != null)
                //{
                //    DataAccess da = new DataAccess();
                //    string loaisp = Request.QueryString["LoaiSP"].ToString();
                //    string sql3 = "SELECT MaSP, HinhAnh, TenSP, DonGia FROM SanPham, LoaiSanPham WHERE SanPham.MaLoai = LoaiSanPham.MaLoai AND TenLoai = '" + loaisp + "'";
                //    dlKQTim.DataSource = da.GetDataList(sql3).Tables[0];
                //    dlKQTim.DataBind();
                //}
            

           
           
     
        }

        protected void btDau_Click(object sender, EventArgs e)
        {
            trang_thu = 0;
            load_KQTim();
        }

        protected void btTruoc_Click(object sender, EventArgs e)
        {
            trang_thu--;
            load_KQTim();
        }

        protected void btSau_Click(object sender, EventArgs e)
        {
            trang_thu++;
            load_KQTim();
        }

        protected void btCuoi_Click(object sender, EventArgs e)
        {
            trang_thu = p.PageCount - 1;
            load_KQTim();
        }

        
    }
}
            
    